﻿#include "ResourceModule.h"

ResourceModule::ResourceModule()
    : m_assetManager(AssetManager::GetInstance()), m_totalAssets(0), m_loadedAssets(0)
{
}

ResourceModule::~ResourceModule()
{
    Shutdown();
}

bool ResourceModule::Initialize()
{
    m_assetManager->RegisterLoadCallback([this](const std::string& id, bool success) {
        OnAssetLoaded(id, success);
        });

    return true;
}

void ResourceModule::Shutdown()
{
    m_assetManager->UnloadAllAssets();
}

bool ResourceModule::LoadFromConfig(const AssetConfig& config)
{
    // 计算要加载的资源总数
    m_totalAssets = static_cast<int>(
        config.textures.size() +
        config.textureAtlases.size() +
        config.audioClips.size() +
        config.textureFlips.size() +
        config.atlasFlips.size()
        );

    m_loadedAssets = 0;

    // 加载纹理
    for (const auto& textureConfig : config.textures)
    {
        auto texture = m_assetManager->CreateAsset<TextureAsset>(
            textureConfig.id, textureConfig.path);
        texture->Load();
    }

    // 加载纹理图集
    for (const auto& atlasConfig : config.textureAtlases)
    {
        auto atlas = m_assetManager->CreateAsset<TextureAtlasAsset>(
            atlasConfig.id, atlasConfig.pathTemplate, atlasConfig.frameCount);
        atlas->Load();
    }

    // 加载音频
    for (const auto& audioConfig : config.audioClips)
    {
        auto audio = m_assetManager->CreateAsset<AudioAsset>(
            audioConfig.id, audioConfig.path);
        audio->Load();
    }

    // 处理纹理翻转
    for (const auto& flipConfig : config.textureFlips)
    {
        auto sourceTexture = m_assetManager->GetAsset<TextureAsset>(flipConfig.sourceId);
        if (sourceTexture)
        {
            // 创建翻转纹理的实现会比较复杂，这里简化处理
            auto flippedTexture = m_assetManager->CreateAsset<TextureAsset>(
                flipConfig.targetId, L"");
            // 加载和翻转纹理的逻辑...
            OnAssetLoaded(flipConfig.targetId, true);
        }
    }

    // 处理图集翻转
    for (const auto& flipConfig : config.atlasFlips)
    {
        auto sourceAtlas = m_assetManager->GetAsset<TextureAtlasAsset>(flipConfig.sourceId);
        if (sourceAtlas)
        {
            auto flippedAtlas = sourceAtlas->CreateFlipped(flipConfig.targetId);
            //m_assetManager->AddExistingAsset(flipConfig.targetId, flippedAtlas);
            OnAssetLoaded(flipConfig.targetId, true);
        }
    }

    return true;
}

std::shared_ptr<TextureAsset> ResourceModule::GetTexture(const std::string& id)
{
    return m_assetManager->GetAsset<TextureAsset>(id);
}

std::shared_ptr<TextureAtlasAsset> ResourceModule::GetTextureAtlas(const std::string& id)
{
    return m_assetManager->GetAsset<TextureAtlasAsset>(id);
}

std::shared_ptr<AudioAsset> ResourceModule::GetAudio(const std::string& id)
{
    return m_assetManager->GetAsset<AudioAsset>(id);
}

void ResourceModule::PlayAudio(const std::string& id, bool loop)
{
    auto audio = GetAudio(id);
    if (audio)
        audio->play(loop);
}

void ResourceModule::StopAudio(const std::string& id)
{
    auto audio = GetAudio(id);
    if (audio)
        audio->pause();
}

void ResourceModule::SetProgressCallback(ProgressCallback callback)
{
    m_progressCallback = callback;
}

void ResourceModule::OnAssetLoaded(const std::string& id, bool success)
{
    m_loadedAssets++;

    if (m_progressCallback && m_totalAssets > 0)
    {
        float progress = static_cast<float>(m_loadedAssets) / m_totalAssets;
        m_progressCallback(progress);
    }
}